package com.gistmap.sideline.exception;

import com.gistmap.sideline.response.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * @author zhangran
 * @date 2018/12/19
 */
@Slf4j
@RestControllerAdvice
public class ExceptionsHandler {


    @ExceptionHandler(Exception.class)
    public Result exception(Exception e) {
        log.error(e.getMessage(), e);
        return new Result().error("Error",500);
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public Result noHandlerFoundException() {
        return new Result().error("Not found",404);
    }

    @ExceptionHandler(ServiceException.class)
    public Result serviceException(ServiceException e) {
        return new Result().fail(e.getErrorCode(), e.getMessage(), e.getCode());
    }
}